package com.chaos.payment.service;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chaos.framework.model.dto.ID;
import com.chaos.framework.model.dto.page.PageList;
import com.chaos.framework.model.dto.page.PageQueryDto;
import com.chaos.framework.model.entity.Table;
import com.chaos.framework.model.helper.ConvertHelper;
import com.chaos.framework.model.helper.PageHelper;
import com.chaos.payment.mapper.TPayWxConfigMapper;
import com.chaos.order.api.data.TPayWxConfigData;

import com.chaos.payment.entity.TPayWxConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
*
* @author yjp
* @since 2024-06-18
*/
@Slf4j
@Component
public class TPayWxConfigService extends ServiceImpl<TPayWxConfigMapper, TPayWxConfig> {

	public ID insertModel(TPayWxConfigData data) {
		TPayWxConfig entity = ConvertHelper.convert(data, TPayWxConfig.class);
		return save(entity) ? new ID(entity.getId()) : null;
	}

	public boolean deleteModel(ID data) {
		return removeById(data.getId());
	}

	public boolean updateModelByID(TPayWxConfigData data) {
		TPayWxConfig entity = ConvertHelper.convert(data, TPayWxConfig.class);
		return update(entity, new UpdateWrapper<TPayWxConfig>().eq(Table.ID, data.getId()));
	}

	public TPayWxConfigData selectByID(ID data) {
		return ConvertHelper.convert(getOne(new QueryWrapper<TPayWxConfig>().eq(Table.ID, data.getId())), TPayWxConfigData.class);
	}

	public List<TPayWxConfigData> selectByData(TPayWxConfigData data) {
		QueryWrapper<TPayWxConfig> query = new QueryWrapper();
		query.orderByDesc(Table.ID);
		return ConvertHelper.convert(list(query), TPayWxConfigData.class);
	}

	public PageList<TPayWxConfigData> selectByPage(PageQueryDto<TPayWxConfigData> pageData) {
		QueryWrapper<TPayWxConfig> query = new QueryWrapper();
		query.orderByDesc(Table.ID);
		return PageHelper.pageList(page(PageHelper.page(pageData), query), TPayWxConfigData.class);
	}

	public TPayWxConfigData getByMerchantId(Long merchantId) {
		Assert.notNull(merchantId, "商户id不能为空");
		return  ConvertHelper.convert(getOne(new LambdaQueryWrapper<TPayWxConfig>().eq(TPayWxConfig::getMerchantId, merchantId)), TPayWxConfigData.class);
	}
}
